<template>
  <div>
    <div style="margin: 10px">
      <span>作业批次选择：</span>
      <Select
        v-model="selectedTask.taskId"
        style="width: 300px"
        v-if="taskLoaded"
        @on-change="changeTaskInfo"
      >
        <Option
          v-for="(item, index) in classTaskList"
          :value="item.taskId"
          :key="index"
          >{{ item.name }}</Option
        >
      </Select>
      <span style="float: right"
        >本批次共提交作业<span
          style="color: blue; font-size: 70; margin: 0 3px"
          >{{ homeworkList.length }}</span
        >份</span
      >
    </div>
    <Table
      border
      :height="contentPageCSS - 145"
      :columns="columns5"
      :data="homeworkList"
    ></Table>
  </div>
</template>

<script>
import { get, post, del, put } from "@/axios";
export default {
  props: ["contentPageCSS"],
  data() {
    return {
      scrollOps: {
        bar: {
          background: "#808695",
        },
      },
      columns5: [
        {
          type: "index",
          width: 60,
          align: "center",
        },
        {
          title: "姓名",
          key: "uploaderName",
        },
        {
          title: "学号",
          key: "uploaderId",
        },
        {
          title: "提交日期",
          key: "createTime",
          sortable: true,
          sortMethod(a, b, type) {
            //自定义排序使用的方法，接收三个参数 a 、 b 和 type，当设置 sortable: true 时有效。type 值为 asc 和 desc
            a = new Date(a);
            b = new Date(b);
            if (type == "asc") {
              return a > b ? 1 : -1;
            } else if (type == "desc") {
              return a > b ? -1 : 1;
            }
          },
          sortType: "asc",
        },

        {
          title: "分数",
          key: "scoreLevel",
          sortable: true,
        },
        {
          title: "评阅意见",
          key: "scoreReview",
        },
      ],
      userInfo: {},
      selectedTask: {},
      classTaskList: [],
      taskLoaded: false,
      homeworkList: [],
      classId: "",
      
    };
  },
  created() {
    // this.userInfo = JSON.parse(localStorage.getItem("userInfo"));
    // if (this.userInfo.role != "admin") {
    //   this.$router.go(-1);
    // }
    this.classId = window.location.href
      .split("/classInfo?id=")[1]
      .split("&content")[0];
  },
  mounted() {
    this.getClassTask();
  },
  methods: {
    async getClassTask() {
      try {
        let res = await get(
          "/visualResourceLibrary/class/task/" + this.classId
        );
        let list = res.data.data;
        for (let i = 0; i < list.length; i++) {
          if (list[i].type == "homework") {
            this.classTaskList.push(list[i]);
          }
        }
        // console.log(this.classTaskList);
        if (this.classTaskList.length > 0) {
          this.selectedTask = JSON.parse(JSON.stringify(this.classTaskList[0]));
          this.getHomework(this.selectedTask.taskId);
        }
        this.taskLoaded = true;
      } catch (e) {
        this.$Message.error("修改失败");
        console.log(e);
      }
    },
    changeTaskInfo(value) {
      for (let i = 0; i < this.classTaskList.length; i++) {
        if (value == this.classTaskList[i].taskId) {
          this.selectedTask = JSON.parse(JSON.stringify(this.classTaskList[i]));
          this.getHomework(this.selectedTask.taskId);
        }
      }
    },
    async getHomework(taskId) {
      // console.log("paths: " + temp);
      let reqInfo = {
        asc: false,
        page: 0,
        pageSize: 150,
      };
      // console.log("paths: " + temp.toString());
      try {
        let res = await post(
          "/visualResourceLibrary/res/resource/browse/" + taskId,
          reqInfo
        );
        // console.log(res);
        let list = res.data.data.content;
        this.homeworkList = [];
        for (let i = 0; i < list.length; i++) {
          if (list[i].uploaderRole == "student") {
            list[i].createTime = new Date(list[i].createTime).toLocaleString();
            this.homeworkList.push(list[i]);
          } else if (list[i].studentUpload == "true") {
            this.getFolderContent(list[i].id);
          }
        }
        // this.homeworkList = res.data.data.content;
      } catch (e) {
        console.log(e);
      }
    },
    async getFolderContent(folderId) {
      // console.log("paths: " + temp);
      let reqInfo = {
        asc: false,
        page: 0,
        pageSize: 150,
      };
      // console.log("paths: " + temp.toString());
      try {
        let res = await post(
          "/visualResourceLibrary/res/resource/browse/" + folderId,
          reqInfo
        );
        // console.log(res);
        let list = res.data.data.content;
        // console.log(list);
        let info = {};
        let foldercontent = [];
        for (let i = 0; i < list.length; i++) {
          if (list[i].uploaderRole == "student") {
            info.uploaderName = list[i].uploaderName;
            info.uploaderId = list[i].uploaderId;
            info.createTime = new Date(list[i].createTime).toLocaleString();
          }
          if (list[i].scoreLevel != "") {
            info.scoreLevel = list[i].scoreLevel;
            info.scoreReview = list[i].scoreReview;
          }
        }
        if (list.length > 0) {
          foldercontent.push(info);
          this.homeworkList = this.mergeLists(foldercontent,this.homeworkList)
          // console.log(this.homeworkList);
          // this.homeworkList.push(info);
        }
        // this.homeworkList = res.data.data.content;
      } catch (e) {
        console.log(e);
      }
    },
    mergeLists(list1, list2){
      let mergedList = list2.slice(); // 复制list2以避免修改原始列表
      let idSet = new Set(mergedList.map(item => item.id));
      list1.forEach(item => {
        const existingIndex = mergedList.findIndex(existingItem => existingItem.uploaderId === item.uploaderId);
        if (existingIndex !== -1) {
            mergedList[existingIndex] = item; // 替换已存在的项
        } else {
            mergedList.push(item); // 添加新的项
        }
    });
    return mergedList;

    }

  },
};
</script>

<style>
  
</style>